package com.hzit.mmcb.core.controller.api;


import com.hzit.common.result.R;
import com.hzit.mmb.base.utils.JwtUtils;
import com.hzit.mmcb.core.hfb.RequestHelper;
import com.hzit.mmcb.core.pojo.vo.UserBindVO;
import com.hzit.mmcb.core.service.UserBindService;
import com.netflix.ribbon.proxy.annotation.Http;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/userBind")
@Api(tags = "会员账号绑定")
@Slf4j
public class ApiUserBindController {
    @Autowired
    private UserBindService userBindService;

    @ApiOperation("账号绑定提交数据")
    @PostMapping("auth/bind")
    public R bind(@RequestBody UserBindVO userBindVO,
                  HttpServletRequest request){
        String token=request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String formStr=userBindService.commitBindUser(userBindVO,userId);
        return R.ok().data("formStr",formStr);
    }

    /**
     * 回调方法
     */
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        if (!RequestHelper.isSignEquals(map)){
            log.error("验签失败");
            return "fail";
        }
        //填写绑定信息
        userBindService.mmcbNotify(map);
        return "success";
    }

}

